/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * resultAndRecipePanel.java
 *
 * Created on 2012-feb-01, 14:11:00
 */
package laboration2.gui;

import java.awt.CardLayout;
import java.awt.event.KeyEvent;
import javax.swing.*;
import se.chalmers.ait.dat215.lab2.Recipe;
/**
 *
 * @author arla
 */
public class ResultAndRecipePanel extends javax.swing.JPanel {

    public void setSearchResults(java.util.List<Recipe> list) {
        resultList.setListData(list.toArray());
        shortResultList.setListData(list.toArray());
    }
    
    /** Creates new form ResultAndRecipePanel */
    public ResultAndRecipePanel() {
        initComponents();
        cardLayout = (CardLayout) this.getLayout();
    }          

    /**
     * Set up the View to show a recipe, selected in the event's source.
     * @param source Hopefully a JList. hehehe.
     */
    private void viewRecipe(JList source) {
        cardLayout.show(this, "resultAndView");
        
        JList sourceList = (JList) source;
        syncListSelectedIndex(sourceList);
        recipeViewPanel.setRecipe((Recipe) sourceList.getSelectedValue());
        
        shortResultList.requestFocusInWindow();
    }

    private void syncListSelectedIndex(JList correctList) {
        JList[] lists = {resultList, shortResultList};
        for (JList list : lists) {
            list.setSelectedIndex(correctList.getSelectedIndex());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        resultList = new javax.swing.JList();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        shortResultList = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        recipeViewPanel = new laboration2.gui.RecipeViewOld();
        recipeToolbar = new javax.swing.JPanel();
        closeButton = new javax.swing.JButton();

        setName("Form"); // NOI18N
        setLayout(new java.awt.CardLayout());

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        resultList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        resultList.setCellRenderer(new RecipeListCell());
        resultList.setName("resultList"); // NOI18N
        resultList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                recipeListMouseClicked(evt);
            }
        });
        resultList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                recipeListKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(resultList);

        add(jScrollPane1, "viewOnly");

        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        shortResultList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        shortResultList.setCellRenderer(new RecipeListCellShort());
        shortResultList.setName("shortResultList"); // NOI18N
        jScrollPane2.setViewportView(shortResultList);

        jSplitPane1.setLeftComponent(jScrollPane2);

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.BorderLayout());

        recipeViewPanel.setName("recipeViewPanel"); // NOI18N
        jPanel1.add(recipeViewPanel, java.awt.BorderLayout.CENTER);

        recipeToolbar.setName("recipeToolbar"); // NOI18N
        recipeToolbar.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEADING));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(laboration2.Laboration2App.class).getContext().getActionMap(laboration2.gui.ResultAndRecipePanel.class, this);
        closeButton.setAction(actionMap.get("closeRecipe2")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboration2.Laboration2App.class).getContext().getResourceMap(laboration2.gui.ResultAndRecipePanel.class);
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        recipeToolbar.add(closeButton);

        jPanel1.add(recipeToolbar, java.awt.BorderLayout.PAGE_END);

        jSplitPane1.setRightComponent(jPanel1);

        add(jSplitPane1, "resultAndView");
    }// </editor-fold>//GEN-END:initComponents

private void recipeListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_recipeListKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        viewRecipe((JList) evt.getSource());
    }
}//GEN-LAST:event_recipeListKeyPressed

private void recipeListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_recipeListMouseClicked
    if (evt.getClickCount() >= 2) {
        viewRecipe((JList) evt.getSource());
    }
}//GEN-LAST:event_recipeListMouseClicked

    @org.jdesktop.application.Action
    public void closeRecipe() {
        syncListSelectedIndex(shortResultList);
        cardLayout.show(this, "resultOnly");
        resultList.requestFocusInWindow(); // keyboard friendly
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JPanel recipeToolbar;
    private laboration2.gui.RecipeViewOld recipeViewPanel;
    private javax.swing.JList resultList;
    private javax.swing.JList shortResultList;
    // End of variables declaration//GEN-END:variables
    
    
    private ListCellRenderer normalRenderer = new RecipeListCell();
    private ListCellRenderer shortRenderer = new RecipeListCellShort();
    private CardLayout cardLayout;
}
